<?php
/**
 * Dillenm-Mvc-Framework
 * 
 * A PHP MVC Framework written by Dillen Meijboom, all rights belong to him.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.library.Form.Validator
 * @url http://code.google.com/p/dillenm-mvc-framework/
 */

/**
 * Check if the element has a value
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.library.Form.Validator
 */

class Dmf_Form_Validator_Upload extends Dmf_Form_Validator
{
    protected $_message = 'The uploaded file is invalid: "%message%"';
    
    public function getRealSize( $size )
    {
        if ( is_numeric( $size ) )
        {
            return $size;
        }
        
        $rsize = substr( $size, 0, -2 );
        
        switch( strtoupper( substr( $size, -2, strlen( $size ) ) ) )
        {
            case 'KB':
                return ( $rsize * 1024 );
                break;
            case 'MB':
                return ( $rsize * 1024 * 1024 );
                break;
            case 'GB':
                return ( $rsize * 1024 * 1024 * 1024 );
                break;
            case 'TB':
                return ( $rsize * 1024 * 1024 * 1024 * 1024 );
                break;
            default:
                return $rsize;
        }
    }
    
    public function isValid( $value )
    {
        $default_options = array(
            'min_size'      => 1,
            'max_size'      => 10000000,
            'mimetypes'     => array(),
            'filetypes'     => array()
        );
        $options = array_merge( $default_options, $this->_value );
        $fsize = @filesize( $value['tmp_name'] );
        $pinfo = pathinfo( $value['name']);
        
        if ( $value['tmp_name'] == null && $value['name'] == null ) return;
        
        if ( $value['error'] != 0 )
        {
            $this->_message = str_replace( '%message%', 'Unknown error' );
            return false;
        }
        
        if ( $fsize < $this->getRealSize( $options['min_size'] ) )
        {
            $this->_message = str_replace( '%message%', 'Filesize is lower then ' . round( ( $this->getRealSize( $options['min_size'] ) / 1024 / 1024 ), 2 ) . ' MB', $this->_message );
            return false;
        }
        
        if ( $fsize > $this->getRealSize( $options['max_size'] ) )
        {
            $this->_message = str_replace( '%message%', 'Filesize is higher then ' . round( ( $this->getRealSize( $options['max_size'] ) / 1024 / 1024 ) ) . ' MB', $this->_message );
            return false;
        }
        
        if ( count( $options['mimetypes'] ) > 0 && ! in_array( strtolower( $value['type'] ), $options['mimetypes'] ) )
        {
            $this->_message = str_replace( '%message%', 'Mimetype is not in list.', $this->_message );
            return false;
        }
        
        if ( count( $options['filetypes'] > 0 ) && ! in_array( strtolower( $pinfo['extension'] ), $options['filetypes'] ) )
        {
            $this->_message = str_replace( '%message%', 'Filetype is not in list.', $this->_message );
            return false;
        }
        
        return true;
    }
}